<?php
/**
 * welcome developer.
 * author: liangbo
 * company:tencent suzhou
 * contact:173120209
 * mobile:18550451730
 * Date: 2016-12-18
 * Time: 4:49
 */

class Controller
{
    protected $_controller;
    protected $_action;
    protected $_view;
    protected $api;

    // 构造函数，初始化属性，并实例化对应模型
    function __construct($controller, $action)
    {
        $this->_controller = $controller;
        $this->_action = $action;
        $this->_view = new View($controller, $action);
        $this->api=false;
    }

    // 分配变量
    function assign($name, $value)
    {
        $this->_view->assign($name, $value);
    }

    // 渲染视图
    function __destruct()
    {
        if($this->api===false)
        $this->_view->render();
    }
    function get($val="",$default="")
    {

       if($val=="")
       {

         return $_GET;

       }else{

          if(isset($_GET[$val])&& !empty($_GET[$val]))
          {


             return $_GET[$val];


          }else{

             return $default;

          }

       }
    }
    function post($val="",$default="")
    {

        if($val=="")
        {

            return $_POST;

        }else{

            if(isset($_POST[$val])&& !empty($_POST[$val]))
            {


                return $_POST[$val];


            }else{

                return $default;

            }

        }
    }
    function isAjax() {
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
            if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
                return true;
        }
        //如果参数传递的参数中有ajax
        if(!empty($_POST['ajax']) || !empty($_GET['ajax']))
            return true;
        return false;
    }
    function islogin()
    {

      $flag=$this->session()->get('myid');
      if(empty($flag))
      {

        echo '<script type="text/javascript">top.location.href="/login";</script>';


      }



    }
    //session
    function session()
    {

        return new session();

    }
    //cookie
    function cookie()
    {


        return new cookie();




    }







}